package com.appdemo.demo.excel;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.xssf.usermodel.*;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ycd0075
 * @date 2021-06-17 14:20
 */
public class ExcelUtil {

    public static String getStringFromJsonFile(String fileName) {
        System.out.println("读取文件...");
        String jsonResult = "";
        try {
            File file = new File(fileName);
            FileReader fileReader = new FileReader(file);
            Reader reader = new InputStreamReader(new FileInputStream(file), "utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonResult = sb.toString();
            System.out.println("读取文件完成");
            return jsonResult;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }


    public static void listToExcel(List<Map> list) {
        System.out.println("数据转换到Excel中...");
        // 定义一个新的工作簿
        XSSFWorkbook wb = new XSSFWorkbook();
        // 创建一个Sheet页
        XSSFSheet sheet = wb.createSheet("First sheet");
        //设置行高
        sheet.setDefaultRowHeight((short) (2 * 256));
        //设置列宽
        sheet.setColumnWidth(0, 4000);
        sheet.setColumnWidth(1, 4000);
        sheet.setColumnWidth(2, 4000);
        XSSFFont font = wb.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 16);
        //获得表格第一行
        XSSFRow row = sheet.createRow(0);
        //根据需要给第一行每一列设置标题
        XSSFCell cell = row.createCell(0);
        cell.setCellValue("ID");
        cell = row.createCell(1);
        cell.setCellValue("ID");
        cell = row.createCell(2);
        cell.setCellValue("ID");
        cell = row.createCell(3);
        cell.setCellValue("ID");
        cell = row.createCell(4);
        cell.setCellValue("ID");
        XSSFRow rows;
        XSSFCell cells;
        //循环拿到的数据给所有行每一列设置对应的值
        for (int i = 0; i < list.size(); i++) {
            // 在这个sheet页里创建一行
            rows = sheet.createRow(i + 1);
            // 该行创建一个单元格,在该单元格里设置值
            String ID = list.get(i).get("ID").toString();
            cells = rows.createCell(0);
            cells.setCellValue(ID);
            cells = rows.createCell(1);
            cells.setCellValue(ID);
            cells = rows.createCell(2);
            cells.setCellValue(ID);
            cells = rows.createCell(3);
            cells.setCellValue(ID);
            cells = rows.createCell(4);
            cells.setCellValue(ID);

        }
        try {
            File file = new File("\\data.xls");
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            wb.write(fileOutputStream);
            fileOutputStream.close();
            System.out.println("数据转换完成");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    public static void main(String[] args) {

        List list = new ArrayList();
        System.out.println("读取文件...");
        String data = getStringFromJsonFile("\\lottery_record_undefeated.0.json");
        System.out.println("读取文件完成");
        JSONObject jsonObject = JSONObject.parseObject(data);
        if (jsonObject.containsKey("data")) {
            JSONObject jsonObject1 = jsonObject.getJSONObject("data");
            if (jsonObject1.containsKey("items")) {
                JSONArray jsonArray = jsonObject1.getJSONArray("items");
                System.out.println("jsonArray.size()--" + jsonArray.size());
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject2 = jsonArray.getJSONObject(i);
                    if (jsonObject2.containsKey("entityInfo")) {
                        Map map1 = new HashMap();
                        JSONObject jsonObject3 = jsonObject2.getJSONObject("entityInfo");
                        String lotteryNumber = "";
                        String campaignId = "";
                        String phone = "";
                        String store = "";
                        String skuOptionValues = "";
                        if (jsonObject3.containsKey("lotteryNumber")) {
                            lotteryNumber = jsonObject3.getString("lotteryNumber");
                        }
                        if (jsonObject3.containsKey("campaignId")) {
                            campaignId = jsonObject3.getString("campaignId");
                        }
                        if (jsonObject3.containsKey("phone")) {
                            phone = jsonObject3.getString("phone");
                        }
                        if (jsonObject3.containsKey("store")) {
                            store = jsonObject3.getString("store");
                        }
                        if (jsonObject3.containsKey("skuOptionValues")) {
                            skuOptionValues = jsonObject3.getString("skuOptionValues");
                        }


                        map1.put("lotteryNumber", lotteryNumber);
                        map1.put("campaignId", campaignId);
                        map1.put("phone", phone);
                        map1.put("store", store);
                        map1.put("skuOptionValues", skuOptionValues);
                        list.add(map1);
                    }
                }
            }
        }
        System.out.println("list:::" + list);
        listToExcel(list);

    }


}
